﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.User;

public partial class user_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null)
        {
            Response.Redirect("profile.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.Params["msg"] != null)
            {
                if (Request.Params["msg"].ToString() == "resend")
                {
                    lblLoginMessage.Text = "Verification email has sent. Please check your inbox or junk box.";
                    lblLoginMessage.ForeColor = System.Drawing.Color.Green;
                    divExErrorMessage.Visible = true;
                }

                
            }

            if (Request.Params["jid"] != null)
            {
                aNewUser.HRef = "../careers/job-submit.aspx?jid=" + Request.Params["jid"].ToString();
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        #region Existing Customer
        if (Page.IsValid)
        {
            AutoLogin(100);
        }
        #endregion
    }

    private void AutoLogin(int LoginType)
    {
        AccountDB _accountdb = new AccountDB();
        //Check Customer Account 
        Account _account = _accountdb.GetUserAccount(txtExEmail.Text.ToLower());

        if (_account != null)
        {
            if (CheckUserLoginCached(_account.UID.ToString()))
            {
                if (Server.HtmlDecode(txtExAuthCode.Text) == _account.AuthCode)
                {
                    if (_account.Status == true)
                    {
                        if (_account.Info1 == "0")
                        {
                            //Reset Email Fail Message
                            _account.Info1 = "";
                            _account.Info2 = "";
                            _accountdb.Insert(_account);
                        }

                        divExErrorMessage.Visible = false;
                        GetUserInformation(_account);
                        CE.Global.General.CreateUpdateUserLoginCached(Page);
                        _account = null;
                        Response.Redirect(GetRedirectPath());
                    }
                    else
                    {
                        divExErrorMessage.Visible = true;
                        lblLoginMessage.Text = "Please verify your account by carefully following the instructions in the email. ";
                        lblLoginMessage.Text += "<br>Click <a href='resend-user-activate.aspx?email=" + _account.Email + "'>here</a> to resend the confirmation email.";
                    }
                }
                else
                {
                    divExErrorMessage.Visible = true;
                    lblLoginMessage.Text = "Email address or password is incorrect.";
                }
            }
            else
            {
                //Response.Redirect("~/User/UserMessage.aspx?mt=UM&ec=1993");
            }
        }
        else
        {
            divExErrorMessage.Visible = true;
            lblLoginMessage.Text = "Email address or password is incorrect.";
        }

        lblLoginMessage.ForeColor = System.Drawing.Color.Red;
    }

    private bool CheckUserLoginCached(string UserId)
    {
        bool _retValue = false;
        string sKey = CE.Configuration.MyConfigurationManager.GetAppSettingValue("UserCacheName");
        sKey += UserId;
        if (HttpContext.Current.Cache[sKey] == null)
        {
            _retValue = true;
        }
        else
        {
            _retValue = false;
        }
        return _retValue;
    }

    private void GetUserInformation(Account _account)
    {
        DateTime dtNow = new DateTime();
        dtNow = DateTime.Now;
        Session[CE.Global.General.UserId] = _account.UID;
        Session[CE.Global.General.UserTypeId] = _account.UserTypeId;

        Session[CE.Global.General.UserEmail] = _account.Email;
        Session[CE.Global.General.StartTime] = dtNow.ToString();
        bool isProfileComplete = false;
        bool isContactComplete = false;

        ProfileDB _profiledb = new ProfileDB();
        Profile _profile = new Profile();

        _profile = _profiledb.GetProfile(_account.UID);

        if (_profile != null)
        {
            Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.Name);

            if (_profile.Status.ToLower() == "complete")
            {
                isProfileComplete = true;
            }
            else
            {
                isProfileComplete = false;
            }
        }
        else
        {
            Session[CE.Global.General.UserName] = null;
        }

        if (isProfileComplete && isContactComplete)
        {
            Session[CE.Global.General.UserProfileAndContactComplete] = "1";
        }
        else
        {
            Session[CE.Global.General.UserProfileAndContactComplete] = "0";
        }

    }

    private string GetRedirectPath()
    {
        string _result = "profile.aspx";

        if (Request.Params["jid"] != null)
        {
            _result = "../careers/job-submit.aspx?jid=" + Request.Params["jid"].ToString();
        }

        return _result;
    }

}

